#include "client.h"

int client_worker(int netfd, char *buf)
{
    char command[8];
    char path[1024];
    
    int length = 0;


    char *command_new = strtok(buf, " ");// 存放用户发送的“命令 路径/文件”中的“命令”
    char *path_new = strtok(NULL, "\n");// 存放用户发送“命令 路径/文件”中的“路径/文件”
    bzero(command, sizeof(command));// 清空command
    bzero(path, sizeof(path));// 清空path
    if (path_new == NULL)// 若用户只发送“命令”未跟路径或文件，如“ls”、“pwd”
    {
        strncpy(command, command_new, strlen(command_new) - 1);// 赋值command（去除\n）
    }
    else
    {
        strcpy(command, command_new);// 赋值command
        strcpy(path, path_new);// 赋值path
    }

    //判断命令
    if (strcmp(command, "puts") == 0)
    {
        printf("----worker_puts_client----\n");
        //worker_puts_client(netfd, path);
    }
    else if (strcmp(command, "gets") == 0)
    {
        printf("----worker_gets_client----\n");
        worker_gets_client(netfd);
    }
    else if (strcmp(command, "cd") == 0 || strcmp(command, "ls") == 0 || strcmp(command, "remove") == 0 || 
            strcmp(command, "pwd") == 0 || strcmp(command, "mkdir") == 0 || strcmp(command, "rmdir") == 0 )
    {
        int length = 0;
        printf("----worker_%s----\n", command);
        recv(netfd, &length, sizeof(length), MSG_WAITALL);

        int total = 0;
        char recvbuf[4096];
        bzero(recvbuf, sizeof(recvbuf));
        char *p = (char *)recvbuf;

        while(total < length)
        {
            ssize_t sret = recv(netfd,p+total,length-total, 0);
            total += sret;
        }
        printf("%s\n", recvbuf);
    }
    else
    {
        printf("----Command is illegal!----\n----Please input again.----\n");
        return -1;
    }

    printf("----------------分隔线------------------\n");
    return 0;
}

// 王
int recvn(int sockfd,void *buf, int length){
    int total = 0;
    char *p = (char *)buf;
    while(total < length){
        ssize_t sret = recv(sockfd,p+total,length-total,0);
        total += sret;
    }
    return 0;
}


int worker_gets_client(int netfd){
    printf("//gets start\n");
    train_t train;
    bzero(&train,sizeof(train));
    recvn(netfd,&train.length,sizeof(train.length));
    recvn(netfd,train.data,train.length);

    char *path = strtok(train.data, "\r\n");
    int fd = open(path,O_RDWR|O_CREAT,0666);
    FILE *fp = fdopen(fd, "r+");

    struct stat statbuf;            //若文件存在 获取文件长度
    fstat(fd,&statbuf);

    fseek(fp,statbuf.st_size,0);    //偏移到文件末尾  
    printf("fseek over \n");                    //
    send(netfd,&statbuf.st_size,sizeof(statbuf.st_size),0); //发送文件长度
                      
    off_t filesize;
    recvn(netfd,&train.length,sizeof(train.length));
    recvn(netfd,train.data,train.length);
    memcpy(&filesize,train.data,sizeof(off_t));
    printf("filesize = %ld\n", filesize);

    //测试单元
    char buf[1024];
    long total = statbuf.st_size;
    while(total < filesize){
        ssize_t sret = recv(netfd,buf,1,0);
        printf("buf%s\n",buf);
        total += sret;
        write(fd,buf,1);
        ERROR_CHECK(sret,-1,"write");
        bzero(buf,sizeof(buf));
        sleep(1);
        printf("filesize - total :%ld -  %ld = %ld\n",filesize,total,filesize-total);
    }

    return 0;
}

//------------------------------------------------------------------------------
// 武
// 客户端注册函数
int c_regist(int sfd){

    char buf[128] = {0};
    char userName[20] = {0};
    char userPasswd[20] = {0};

    printf("Regist a new user\n\n");
    printf("Please input new user name: ");
    scanf("%s", userName);

    printf("Please input new user password: ");
    scanf("%s", userPasswd);

    sprintf(buf, "%s%s%s", userName, " ", userPasswd);

    puts(buf);
    
    int length = strlen(buf);
    send(sfd, buf, length, 0);

    int confirm = 0;
    //接收服务端的确认信息；
    recv(sfd, &confirm, 4, 0);

    if(confirm == 0){
        printf("username already exists!\n");
    }
    else{
        printf("Regist success\n");
    }

    return 0;
}



//武 客户端登录函数
int c_login(int sockfd, int *isLogin){


    char buf[128] = {0};
    char userName[20] = {0};
    char userPasswd[20] = {0};

    printf("Please input user name: ");
    scanf("%s", userName);

    printf("Please input password: ");
    scanf("%s", userPasswd);

    sprintf(buf, "%s%s%s", userName, " ", userPasswd);
    //
    puts(buf);

    int length = strlen(buf);
    send(sockfd, &length, sizeof(length), 0);
    send(sockfd, buf, length, 0);
    //给客户端识别是否登录成功的标识
    int confirm = 0;

    recv(sockfd, &confirm, 4, 0);

    if(confirm == 0){
        printf("login failed\n");
    }
    else{
        printf("login success\n");
        *isLogin = 1;
    }

    return 0;
}
